package top.wjgyy.tools.ddns.job;

import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import javafx.application.Platform;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import top.wjgyy.tools.ddns.model.IndexModel;
import top.wjgyy.tools.ddns.service.DDNSService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DDNSJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        IndexModel model = (IndexModel)context.getMergedJobDataMap().get("model");
        DDNSService instance = DDNSService.getInstance(model.getAccessKeyId(), model.getSecret());
        DescribeDomainRecordsResponse.Record record = model.getDomainRecordsComboValue();
        String type = model.getDomainTypeComboValue().getType();
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));

        String currentHostIP = "";
        switch (type){
            case "A":
                currentHostIP = model.getIpv4();
                break;
            case "AAAA":
                currentHostIP = model.getIpv6();
                break;
            default:
                Platform.runLater(() -> model.addHistoryValue(format + "----" + "暂不支持此类型！"));

                return;
        }
        String str = instance.updateBindIp(record.getDomainName(), record.getRR(), model.getDomainTypeComboValue().getType(), currentHostIP);
        Platform.runLater(() -> model.addHistoryValue( format + "----" + str));
    }
}
